package design.simdi.code.final_paper.site.panels.licente.proiecte;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import design.simdi.code.final_paper.core.entities.Bibliography;
import design.simdi.code.final_paper.core.entities.Project;
import design.simdi.code.final_paper.site.panels.PagePanel;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 12:20:14 AM)
 * 
 */
public class ViewProjectDetails extends PagePanel {

	private static final long serialVersionUID = 1L;
	private Project project;

	private Label name;
	private MultiLineLabel description;
	private MultiLineLabel notes;
	private Label keywords;
	private AjaxFallbackButton back;
	@SuppressWarnings("unchecked")
	private Form form;

	public ViewProjectDetails(String id, IModel<Project> model) {
		super(id, model);

	}

	@SuppressWarnings("unchecked")
	@Override
	public void panelInit() {
		setOutputMarkupId(true);

		project = (Project) getDefaultModelObject();

		form = new Form("form");

		form.add(name = new Label("name", new PropertyModel<String>(project, "name")));
		form.add(description = new MultiLineLabel("description", new PropertyModel<String>(project, "description")));
		form.add(notes = new MultiLineLabel("notes", new PropertyModel<String>(project, "notes")));
		form.add(keywords = new Label("keywords", new PropertyModel<String>(project, "keywordsAsString")));
		List<Bibliography> listaBibliografii = new ArrayList<Bibliography>(project.getBibliographies());

		form.add(new ListView<Bibliography>("lista-bibliografie", listaBibliografii) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Bibliography> item) {
				Bibliography bibliography = item.getModelObject();
				item.add(new Label("bibliography-title", (item.getIndex() + 1) + ". " + bibliography.getName()));
				item.add(new ExternalLink("bibliography-link", bibliography.getLink(), bibliography.getLink()));
				item.add(new MultiLineLabel("bibliography-description", bibliography.getDescription()));
			}
		});

		name.setEnabled(false);
		description.setEnabled(false);
		keywords.setEnabled(false);
		notes.setEnabled(false);

		add(form);
		back = new AjaxFallbackButton("cancel", form) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				ProiecteTabPanel panel = new ProiecteTabPanel("panel");
				panel.setOutputMarkupId(true);

				Page page = this.getPage();

				page.get("content:tabs:panel").replaceWith(panel);

				target.addComponent(page);
			}
		};
		form.add(back);

	}
}
